package java_thinking.unit_21.Exercise;

import org.junit.Test;

/**
 * @Desc:
 * @author: cww
 * @DateTime: 2019-07-21 18:41
 */

public class StaticExerciseTest<E> {
    /**
     * 测试引用
     */
    @Test
    public synchronized void testRef() {
        Node<String> head ;
        Node<String> tail ;
        tail = head = new Node<>(null);
        Node<String> e1 = new Node<>("e1");
        tail.next = e1;
        System.out.println(tail.item);
        Node<String> t = tail;
        Node<String> p = t;
        Node<String> q = p.next;
        System.out.println(t.toString());
        System.out.println(p.toString());
        System.out.println(q.toString());
//        tail = new Node<>("newTail");
//        System.out.println(p != t);
//        System.out.println(t != (t = tail));
        System.out.println("************");
        p = q;
        System.out.println(p.toString());
        System.out.println(q);

    }

    @Test
    public void testFor() {
        Node<String> head ;
        Node<String> tail ;
        tail = head = new Node<>(null);
        int i = 0;
        Node<String> e1 = new Node<>("e1");
        tail.next = e1;
        for (Node<String> t = tail, p = t;;) {
            Node<String> q = p.next;
            System.out.println("t: " + t.toString());
            System.out.println("p: " + p.toString());
            System.out.println("q: " + q);
            p = q;
            System.out.println("p: " + p.toString());
            System.out.println("q: " + q);
            System.out.println("***************");
            i++;
            if (i>5) {
                break;
            }
        }
    }
}

class Node<E> {
    volatile E item;
    volatile Node<E> next;

    public Node(E item) {
        this.item = item;
    }

    @Override
    public String toString() {
        return "Node{" +
                "item=" + item +
                ", next=" + next +
                '}';
    }
}